<?php

/*	
['.DB_NAME_ADMIN.'] (C)2007 aodian Co,Ltd.
$File: storage.class.php $
*/
if (!defined('ADMIN_INIT'))
	exit ('Access Denied!');

// ---------- class storage --------------------
class storage {

	private $db = null;

	public function __construct($db) {
		$this->db = $db;
	}

	public function __destruct() {
		unset ($this->db);
	}

	public function goods_sortList() {
		$sql = 'CALL '.DB_NAME_ADMIN.'.sp_goods_sortList();';
		$result = $this->executeSQL($sql);
		if (is_array($result)){
			return $result;
		}
		return false;
	}

	public function goods_post($sortID, $name, $value, $intro, $price, $discount, $genre) {
		if ($sortID > 0 && $price >= 0 && $discount >= 0 && $genre >= 0) {
			$sql = 'CALL '.DB_NAME_ADMIN.'.sp_goods_post(' . $sortID . ',"' . $name . '",' . $value . ',"' . $intro . '",' . $price . ',' . $discount . ',' . $genre . ');';
			$result = $this->executeSQL($sql);
			if ($result != false){
				return $result;
			}
		}
		return false;
	}

	public function goods_genre($goodsID) {
		if ($goodsID > 0) {
			$sql = 'CALL '.DB_NAME_ADMIN.'.sp_goods_genre(' . $goodsID . ');';
			$result = $this->executeSQL($sql);
			if ($result != false){
				return $result;
			}
		}
		return false;
	}

	public function goods_modify($goodsID, $name, $value, $intro, $price, $discount) {
		if ($goodsID > 0 && $name != '' && $value >= 0 && $price >= 0 && $discount >= 0) {
			$sql = 'CALL '.DB_NAME_ADMIN.'.sp_goods_modify(' . $goodsID . ',"' . $name . '",' . $value . ',"' . $intro . '",' . $price . ',' . $discount . ');';
			$result = $this->executeSQL($sql);
			if ($result != false){
				return $result;
			}
		}
		return false;
	}

	public function goods_del($goodsID) {
		if ($goodsID > 0) {
			$sql = 'CALL '.DB_NAME_ADMIN.'.sp_goods_del(' . $goodsID . ');';
			$result = $this->executeSQL($sql);
			if ($result != false){
				return $result;
			}
		}
		return false;
	}

	public function goods_list($wd, $rID, $sID, $oID) {
		if ($rID >= 0 && $sID >= 0 && $oID >= 0) {
			$sql = 'CALL '.DB_NAME_ADMIN.'.sp_goods_list("' . $wd . '",' . $rID . ',' . $sID . ',' . $oID . ',NULL,1);';
			$tmpResult = $this->getAllSQL($sql);
			$total = $tmpResult[0][0];
			if ($total > 0) {
				$pageArr = $this->showPage($total, 20);
				$sql = 'CALL '.DB_NAME_ADMIN.'.sp_goods_list("' . $wd . '",' . $rID . ',' . $sID . ',' . $oID . ',"' . $pageArr['limit'] . '",2);';
				$arrRow = $this->getAllSQL($sql);
				if (is_array($arrRow)) {
					$arrRow['cnt'] = count($arrRow);
					$arrRow['page'] = $pageArr['page'];
					return $arrRow;
				}
			}
		}
		return false;
	}

	public function goods_showSingle($goodsID) {
		if ($goodsID > 0) {
			$sql = 'CALL '.DB_NAME_ADMIN.'.sp_goods_showSingle(' . $goodsID . ');';
			$result = $this->executeSQL($sql);
			if (is_array($result)){
				return $result;
			}
		}
		return false;
	}
	
	private function executeSQL($sql){
		if ($sql != ''){
			$result = $this->db->Execute($sql);
			if (is_object($result)) {
				$arrRow = $result->FetchRow();
				$this->db->NextResult();
				return $arrRow[0];
			}
		}
		return false;
	}

	private function getAllSQL($sql){
		if ($sql != ''){
			$result = $this->db->GetAll($sql);
			if (is_array($result)) {
				$this->db->NextResult();
				return $result;
			}
		}
		return false;
	}

	private function showPage($total, $perpage = 10) {
		if ($total > 0) {
			require_once ('page.class.php');
			$page = new extpage(array (
				'total' => $total,
				'perpage' => $perpage
			));
			$pageArr['page'] = $page->show();
			$pageArr['limit'] = $page->limit();
			unset ($page);
		}
		return $pageArr;
	}
}
?>